package com.maxh.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.maxh.pojo.Admin;
import com.maxh.pojo.Teacher;
import com.maxh.pojo.User;
import com.maxh.service.TokenService;
import org.springframework.stereotype.Service;

import java.util.Date;

/***
 * token 下发
 * @Title: TokenService.java
 * @author MRC
 * @date 2019年5月27日 下午5:40:25
 * @version V1.0
 */
@Service("TokenService")
public class TokenServiceImpl implements TokenService {

    /*获取token*/
    public String getToken(User user) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 1000 * 24;//有效时间：24小时
        Date end = new Date(currentTime);
        String token = "";
        token = JWT.create().withAudience(user.getId()+"").withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }

    @Override
    public String getAdminToken(Admin admin) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 1000 * 24;//有效时间：24小时
        Date end = new Date(currentTime);
        String token = "";
        token = JWT.create().withAudience("admin"+admin.getId()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(admin.getPassword()));
        return token;
    }

    @Override
    public String getTeacherToken(Teacher teacher) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 1000 * 24;//有效时间：24小时
        Date end = new Date(currentTime);
        String token = "";
        token = JWT.create().withAudience("teacher"+teacher.getId()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(teacher.getPassword()));
        return token;
    }

    @Override
    public String getStudentToken(User user) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 1000 * 24*365;//有效时间：24小时
        Date end = new Date(currentTime);
        String token = "";
        token = JWT.create().withAudience("student"+user.getId()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }
}
